package com.tianxin.workbao.workBao.fragment;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.BackBillAdapter;
import com.tianxin.workbao.workBao.bean.OutputBill;
import com.tianxin.workbao.workBao.dataProvide.OutputProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.DensityUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hacker on 2017/7/18.
 */

public class BackBillFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, UpdateListViewListener {

    private OutputProvide mProvide;
    private BackBillAdapter mAdapter;
    private RecyclerViewScrollListener recyclerViewScrollListener;
    private int currPage = 1;
    private Integer mStatus;
    private Context mContext;

    private List<OutputBill> mDatas;
    private boolean isHasData = true;

    public static BackBillFragment newInstance(int status) {

        Bundle args = new Bundle();
        args.putInt("status", status);
        BackBillFragment fragment = new BackBillFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        this.mStatus = bundle.getInt("status");
        mDatas = new ArrayList<>();
        mContext = getContext();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void initRootView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.common_content, container, false);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.recycler_color1, R.color.recycler_color2,
                R.color.recycler_color3, R.color.recycler_color4);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        //设置布局管理器
        mLayoutManager = new GridLayoutManager(getActivity(), spanCount);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return mAdapter.getItemColumnSpan(position);
            }
        });
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new BackBillAdapter(mDatas, getContext(), mStatus);
        mRecyclerView.setAdapter(mAdapter);
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void initEvents() {
        //设置监听RecyclerView 的滚动监听事件
        mRecyclerView.addOnScrollListener(new RecyclerViewScrollListener());
    }

    @Override
    public void search(String query) {
        getmProvide().search(query);
    }

    @Override
    public void initData(boolean isSavedNull) {
        onRefresh();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    /**
     * get data
     */
    @Override
    public void onRefresh() {
        if (mSwipeRefreshLayout.isRefreshing() == false) {
            mSwipeRefreshLayout.setRefreshing(true);
            getmProvide().getBackBill(mStatus, currPage = 1);
        }
        mSwipeRefreshLayout.setRefreshing(false);
    }


    @Override
    public void showMessage(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void showProgress() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideProgress() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefreshData(Object result) {
        mDatas.clear();
        if (result != null) {
            mDatas.addAll((List<OutputBill>) result);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMoreData(Object result) {
        if (isHasData == false) {
            return;
        }
        if (result.equals(null)) {
            isHasData = false;
            return;
        } else {
            isHasData = true;
        }
        List<OutputBill> list = (List<OutputBill>) result;
        if (list.size() > 0) {
            mDatas.addAll(list);
            mAdapter.notifyItemRangeInserted(mDatas.size(), list.size());
        }
    }


    class RecyclerViewScrollListener extends RecyclerView.OnScrollListener {
        private int lastVisibleItem;
        private int mScrollThreshold = DensityUtils.dp2px(getActivity(), 1);

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount()) {
                loadMore();
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            boolean isSignificantDelta = Math.abs(dy) > mScrollThreshold;
            lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
        }
    }

    /**
     * 加载 更多
     */
    public void loadMore() {
        if (isHasData) {
            mSwipeRefreshLayout.setRefreshing(true);
            currPage++;
            getmProvide().getBackBill(mStatus, currPage);
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    public OutputProvide getmProvide() {
        if (mProvide == null) {
            mProvide = new OutputProvide(getContext(), this);
        }
        return mProvide;
    }
}
